package piis;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.swing.JFrame;

import org.jfree.ui.RefineryUtilities;

import Algorithms.LinearRegression;
import NeuronNetworkLibrary.Network;

public class LinearRegressionOther {

	public final static int EPOCH = 5;
    public final static double ERROR = 0.000001;
	
	public static void main(String[] args) {
			

		
			
		
			double[][] obtainedResults = new double[1][100];
			double[] tset = new double[100];
			
			for(int i = 0; i < 100; i++){
				Random generator = new Random();
				double number = 2 - (generator.nextDouble()*(4));
				tset[i] = number;
				obtainedResults[0][i] = function1(number);
			}
			

	        
	        //Number or parameters(degree of the poly)
	        
            int numb = 4;
                
	        //Prepare the X matrix with the training set
	        double[][] x = new double[numb][tset.length];
	        for(int i = 1; i < numb ; i++){
	        	for(int j=0;j<tset.length; j++){
	        		x[i-1][j] = Math.pow(tset[j], i);
	        	}
	        }
	        //Prepare the Y matrix with the obtainedResults
	        double[] y = obtainedResults[0];
	        
	        //Create the linealRegression with this two matrix
	        
	        LinearRegression linealR = new LinearRegression(x,y,numb);
	        
	        //Execute the algorithm
	        linealR.gradientDescent();
	        
	        
	        
	
	        // Preparation of data for the plot.
	        Map<Double,Double> map = new HashMap<>();
	       	try{
		       	FileWriter paramsF = new FileWriter("src/prueba_result.out");
	        for(double j = -2; j <= 2; j+=0.01){
	        	map.put(Double.valueOf(j), linealR.func(j));
	        	paramsF.write(j + " " + linealR.func(j) + "\r\n");
	             
	        }  paramsF.close();
	        }catch(Exception e){
             	e.printStackTrace();
	     
	        }
	        double[][] trainingSet = new double[1][100];
	        trainingSet[0] = tset;
	        
	        // Create a plot.
	        Plot demo = new Plot("Assignment 2","Sinus",map,obtainedResults,trainingSet);
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        demo.setVisible(true);
		        	
	        double[] errors = linealR.getErrors();
	        try{
	        	FileWriter errorsF = new FileWriter("src/prueba_train.out");
	          	             
	  	        for(int i1 = 0; i1<errors.length; i1++){
	  	        	errorsF.write(errors[i1] + "\r\n");
	  	        }
	        	
	  	        errorsF.close();
	  	        
	        }catch(Exception e1){
	        	e1.printStackTrace();
	        }
	        
		}
	 
	
	    private static double[][] printFinalResults(Network network) {

	        int patternNumber = network.getNumberOfPatterns();
	        int patternLength = network.getOutputLayer().size();
	        
	        double[][] obtainedResults = new double[patternNumber][patternLength];
	        for (int i = 0; i < network.getNumberOfPatterns(); i++) {
	            network.calculateNetwork(i);

	            for (int j = 0; j < network.getOutputLayer().size(); j++) {
	                obtainedResults[i][j] = network.getOutputLayer().get(j).getCalculatedOutput();
	            }
	        }
	        
	        return obtainedResults;
	    }
	    
		private static double function1(double x){
			Random generator = new Random();
			double number = 2 - (generator.nextDouble()*(4));
			double result = Math.pow(x, 2) + x + number;
			return result;
		}

}
